package com.mall.order.mq;

import com.mall.order.OrderCoreService;
import com.mall.order.dto.CancelOrderRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @program: cs-mall
 * @description:
 * @author: 热心市民小刘
 * @create: 2021-04-26 17:37
 **/
@Slf4j
@Component
public class DelayCancelOrderConsumer {

    @Autowired
    OrderCoreService orderCoreService;


    //todo: 为什么没有使用Autowired
    private DefaultMQPushConsumer consumer;

    @PostConstruct
    public void init() {
        consumer = new DefaultMQPushConsumer("delay_consumer");
        consumer.setNamesrvAddr("127.0.0.1:9876");
        try {
            consumer.subscribe(DelayCancelOrderProducer.DELAY_ORDER_TOPIC, "*");
            consumer.registerMessageListener(new MessageListenerConcurrently() {
                @Override
                public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list,
                                                                ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                    try {
                        //获取消息
                        MessageExt messageExt = list.get(0);
                        //从消息中获取带取消id
                        byte[] body = messageExt.getBody();
                        String orderId = new String(body, 0, body.length, "utf-8");

                        int id = Integer.parseInt(orderId);
                        //todo: 业务逻辑

                        //  1 根据订单id查询订单支付状态  tb_order   status ☞  （通过 order.biz.constants.OrderConstants）
                        int num = orderCoreService.checkOrderStatus(id);
                        //  2 订单已支付成功 什么都不做

                        //  3 否则，该订单在规定时间还未支付，就准备取消订单
                        if (num == 0) {
                            CancelOrderRequest cancelOrderRequest = new CancelOrderRequest();
                            cancelOrderRequest.setOrderId(orderId);
                            //todo: 缺少会返回值？

                            //      a 修改orderId对应的订单条目，状态为交易取消
                            //      b 将每一个订单中的每一个订单条目锁定的库存，在库存表中还原回去，解锁相应库存
                            //      c 修改订单条目中订单的状态 为库存以释放(order_itam)
                            orderCoreService.cancelOrder(cancelOrderRequest);


                        }

                    } catch (UnsupportedEncodingException e) {
                        return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                    }
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
            });
        } catch (MQClientException e) {
            e.printStackTrace();
        }

    }

}
